package com.qf06;

public class BankCard {
    private double money;
    private Boolean flag=false;//true表示有钱可以取钱 false没钱可以存取
     //存钱
    public synchronized void save(double m){
        while (flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        money=money+m;
        System.out.println(Thread.currentThread().getName()+"存了"+m+"余额是"+money);
        flag=true;
        this.notifyAll();
    }
    //取钱
    public synchronized void take(double m){
        while (!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        money=money-m;
        System.out.println(Thread.currentThread().getName()+"取了"+m+"余额是"+money);
        flag=false;
        this.notifyAll();
    }

}
